<template>
  <c-view>
    <template v-slot:header>
      <div class="title">
        {{ $route.meta.title }}
        <el-button type="primary" :size="size" icon="el-icon-plus" @click="showDialog">新增调度员</el-button>
      </div>
    </template>
    <div class="main__box">
      <c-table
        ref="cTable"
        selection
        hasBorder
        :max-height="maxHeight"
        :size="size"
        :loading="isLoading"
        :table-header="tableHeader"
        :table-list="tableList"
        :table-inner-btns="tableInnerBtns"
        :page-info="pageInfo"
        @change-pagination="changePagination"
      >
        <template v-slot:header>
          <c-search :form-model="searchObj" :form-items="searchItems" @submit-form="searchSubmit" @reset-form="searchReset"></c-search>
        </template>
      </c-table>
    </div>
    <div v-if="dialogObj.isShow">
      <c-dialog
        class="fullscreen-dialog"
        :is-show="dialogObj.isShow"
        :title="dialogObj.title"
        :fullscreen="dialogObj.showType === 1"
        :no-btn="dialogObj.showType === 1"
        close-btn
        @before-close="dialogObj.isShow = false"
        @on-submit="dialogConfirm"
      >
        <earnings ref="childRef" :init-data.sync="dialogObj.initData" v-if="dialogObj.showType === 1"></earnings>
        <dispatcher-add ref="childRef" :init-data.sync="dialogObj.initData" v-else :is-edit="dialogObj.isEdit"></dispatcher-add>
      </c-dialog>
    </div>
  </c-view>
</template>

<script>
import mixinTable from 'mixins/table'
import CDialog from 'components/dialog'
import dictObj from '@/store/dictData'
import dispatcherAdd from './add'
import earnings from './earnings'

export default {
  name: 'dispatcher',
  mixins: [mixinTable],
  components: {
    CDialog,
    dispatcherAdd,
    earnings
  },
  data(vm) {
    return {
      // 对话框对象
      dialogObj: {},
      // 表格内操作按钮
      tableInnerBtns: [
        {
          name: '编辑',
          icon: 'el-icon-edit',
          width: 230,
          handle(row) {
            const { id, name, phone, companyId, companyName, status, type } = row
            vm.showDialog({
              title: '编辑调度员',
              isShow: true,
              isEdit: true,
              initData: {
                id,
                name,
                phone,
                companyId,
                companyName,
                status,
                type
              }
            })
          }
        },
        {
          name: '查看收益',
          icon: 'el-icon-view',
          handle(row) {
            const { id } = row
            vm.showDialog({
              title: '查看收益',
              isShow: true,
              showType: 1,
              initData: {
                id
              }
            })
          }
        },
        {
          name: '查看提现',
          icon: 'el-icon-view',
          handle(row) {
            vm.routerLink(`/vehicle/dispatcher/widthdraw/${row.id}`)
          }
        }
      ],
      tableHeader: [
        {
          label: '所属公司',
          prop: 'companyName'
        },
        {
          label: '调度员',
          prop: 'name',
          search: {
            type: 'input'
          }
        },
        {
          label: '手机号',
          prop: 'phone'
        },
        {
          label: '类型',
          prop: 'type',
          formatter: row => row && vm.setTableColumnLabel(row.type, 'dispatcherType'),
          search: {
            type: 'dict',
            optionsList: dictObj.dispatcherType
          }
        },
        {
          label: '状态',
          prop: 'status',
          formatter: row => row && vm.setTableColumnLabel(row.status, 'disabledStatus'),
          setColor: row => (row.status === 0 ? 'red' : ''),
          search: {
            type: 'dict',
            optionsList: dictObj.disabledStatus
          }
        },
        {
          label: '创建时间',
          prop: 'createTime',
          width: 160
        },
        {
          label: '创建人',
          prop: 'createName'
        },
        {
          label: '更新时间',
          prop: 'updateTime',
          width: 160
        },
        {
          label: '更新人',
          prop: 'updateName'
        }
      ]
    }
  },
  created() {
    this.fetchData()
  },
  methods: {
    /*
     * 查询表格列表数据
     */
    fetchData() {
      const { pageNo, pageSize } = this.pageInfo
      const { dateTime, ...other } = this.searchObj
      const searchDate = this.getSearchDate(dateTime, 'dateTime')
      this.isLoading = true
      this.$api.vehicle
        .fetch('dispatcherList', {
          ...searchDate,
          ...other,
          pageNo: pageNo,
          pageSize: pageSize
        })
        .then(res => {
          this.isLoading = false
          if (res && res.totalCount) {
            const { data, totalCount } = res
            this.pageInfo.totalNum = totalCount
            this.tableList = data || []
          } else {
            this.tableList = res || []
          }
        })
    },
    /**
     * dialog对话框数据处理
     * @opts {*}
     */
    showDialog(opts) {
      this.dialogObj = {
        isShow: true,
        isEdit: opts.isEdit || false,
        showType: opts.showType,
        title: opts.title || '新增调度员',
        initData: opts.initData
      }
    },
    /**
     * dialog新增、编辑处理函数
     */
    dialogConfirm() {
      const childRef = this.$refs.childRef
      childRef.$refs.formRef.validate(valid => {
        if (valid) {
          const params = childRef.formModel
          const curAction = this.dialogObj.isEdit ? 'updateDispatcher' : 'insertDispatcher'
          this.$api.vehicle.fetch(curAction, params).then(() => {
            this.responeHandle(this.dialogObj.isEdit ? '更新成功' : '新增成功')
          })
        } else {
          console.log('error submit!!')
          return false
        }
      })
    }
  }
}
</script>
